package henu.cn.androidhomework;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class GouWuFragment extends Fragment {
    // 缓存Fragment view
    private View rootView;
    private static GouWuFragment gouWuFragment;
    public GouWuFragment(){}
    public static GouWuFragment getNewInstance(){
        if (gouWuFragment ==null){
            gouWuFragment =new GouWuFragment();
        }
        return gouWuFragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.activity_gou_wu_fragment, container, false);
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        ListView listView=(ListView) rootView.findViewById(R.id.listView);

        final List<String> list=new ArrayList<String>();
        list.add("生活笔记");
        list.add("旅行笔记");
        list.add("学习笔记");

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);

        AdapterView.OnItemClickListener listener=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch(position){
                    case 0://进入生活笔记界面
                        Toast.makeText(getActivity(),list.get(position).toString(),Toast.LENGTH_LONG).show();
                        break;
                    case 1://进入旅行笔记界面
                        Intent intent=new Intent(getActivity(),yemian5.class);
                        startActivity(intent);
                        break;
                    case 2://进入学习笔记界面
                        break;
                    case 3://
                        break;
                    case 4://
                        break;
                    case 5://
                        break;
                    case 6://
                        break;
                }
            }
        };
        listView.setOnItemClickListener(listener);

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }


}